package com.example.demo.paint;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class PaintPicture {
    public static void main(String[] args) throws IOException {
        demo();
    }

    public static void demo() throws IOException {
        //创建图片
        //默认背景色是黑色的
        BufferedImage bufferedImage = new BufferedImage(1200, 1200, BufferedImage.TYPE_INT_RGB);


        //获取图片的画笔
        Graphics2D graphics = bufferedImage.createGraphics();

        //消除文字锯齿
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //消除画图锯齿
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //设置背景色
        graphics.setBackground(Color.WHITE);
        //通过使用当前绘图表面的背景色进行填充来清除指定的矩形
        graphics.clearRect(0, 0, 500, 1200);

        //设置背景色
        graphics.setBackground(Color.CYAN);
        //通过使用当前绘图表面的背景色进行填充来清除指定的矩形
        graphics.clearRect(500, 0, 500, 1200);


        //设置画笔的文字类型
        Font font = new Font("黑体", Font.ITALIC, 26);
        graphics.setFont(font);

        //设置画笔的颜色
        graphics.setColor(Color.green);

        //写入文字
        graphics.drawString("你好,今天星期五", 120, 100);

        //画横线
        graphics.setColor(Color.GRAY);
        graphics.drawLine(80, 110, 420, 110);

        //3D矩形
        graphics.draw3DRect(70, 120, 100, 100, true);

        //矩形
        graphics.drawRect(70, 500, 100, 200);

        //填充有背景色的矩形
        GradientPaint gradientPaint = new GradientPaint(5, 40, Color.RED, 15, 50, Color.YELLOW, true);
        graphics.setPaint(gradientPaint);
        graphics.fillRect(70, 800, 100, 200);


        //圆形 startAngle：起始角度，arcAngle：结束角度
        graphics.setColor(Color.MAGENTA);
        graphics.drawArc(70, 300, 100, 100, 0, 360);

        //填充有背景色的圆形
        graphics.setPaint(Color.orange);
        graphics.fillArc(180, 300, 100, 100, 0, 360);


        //获取图片
        BufferedImage read = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\测试素材\\6590.jpg"));

        //填充图片,起始位置为（500,100的位置），宽高为图片的宽高
        graphics.drawImage(read, null, 500, 100);

        //填充图片，起始位置为（500,100的位置），宽高均为100
        graphics.drawImage(read, 500, 800, 100, 100, null);


        //立方体
        //上面的矩形
        //上
        graphics.drawLine(300, 550, 400, 550);
        //左
        graphics.drawLine(300, 550, 180, 600);
        //右
        graphics.drawLine(400, 550, 280, 600);
        //下
        graphics.drawLine(180, 600, 280, 600);

        //中间的四条线
        graphics.drawLine(180, 600, 180, 700);
        graphics.drawLine(280, 600, 280, 700);
        graphics.drawLine(400, 550, 400, 650);
        graphics.drawLine(300, 550, 300, 650);

        //下面的矩形
        //上
        graphics.drawLine(300, 650, 400, 650);
        //左
        graphics.drawLine(300, 650, 180, 700);
        //右
        graphics.drawLine(280, 700, 400, 650);
        //下
        graphics.drawLine(180, 700, 280, 700);


        //球

        //心


        //绘制员
        //圆心坐标
        int a = 50;
        int b = 250;
        int r = 30;
        //绘制由指定的 Polygon对象定义的多边形的轮廓。
        Polygon p = new Polygon();
        for (int x = 20; x <= 80; x++) {
            int i = r * r - (x - a) * (x - a);
            double sqrt = Math.sqrt(i);
            double v = sqrt + b;
            p.addPoint(x, new Double(v).intValue());
        }
        for (int x = 80; x >= 20; x--) {
            int i = r * r - (x - a) * (x - a);
            double sqrt = Math.sqrt(i);
            double v2 = -sqrt + b;
            p.addPoint(x, new Double(v2).intValue());
        }

        graphics.setColor(Color.pink);
        graphics.drawPolygon(p);

        //drawPolygon是按照点添加的顺序来进行链接的
        Polygon p1 = new Polygon();
        p1.addPoint(100,100);
        p1.addPoint(200,100);
        p1.addPoint(200,200);
        p1.addPoint(100,200);
        graphics.drawPolygon(p1);


        //写出图片
        File file = new File("C:\\Users\\Administrator\\Desktop\\测试素材\\123.jpg");
        try {
            ImageIO.write(bufferedImage, "jpg", file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //释放对象
        graphics.dispose();
    }

}
